package com.edata.bigdata.utils

import org.apache.spark.sql.functions
import org.locationtech.proj4j.{CRSFactory, CoordinateTransformFactory, ProjCoordinate}

object GeoUDF {

  val transformPointByEPSG = functions.udf((x: String,y:String, srcEPSG: Int, dstEPSG: Int) => {
    val crsFactory = new CRSFactory()
    val srcCRS = crsFactory.createFromName(s"EPSG:${srcEPSG}")
    val dstCRS = crsFactory.createFromName(s"EPSG:${dstEPSG}")
    val ctFactory = new CoordinateTransformFactory()
    val transform = ctFactory.createTransform(srcCRS,dstCRS)
    val srcCoord = new ProjCoordinate(x.toDouble,y.toDouble)
    val dstCoord = new ProjCoordinate()
    transform.transform(srcCoord,dstCoord)
    s"POINT(${dstCoord.x} ${dstCoord.y})"
  })

}